<?php

namespace Sbm\ControlPanel;

use Sbm;

$layout->AddCSS('./mods/profession/profession.css');

$data = array();

$search = new Sbm\ProfessionSearch($db);

$data = $search->GetByFilters(array(
	'orderby' => '`title` ASC'
		));

$pids = array();
foreach ($data as $profession)
{
	$pids[] = $profession->id;
}

$search2 = new Sbm\MasterSearch($db);
$data1 = $search2->GetByProfessions($pids);

foreach ($data as $pid => $master)
{
	$data[$pid]->masters = $data1[$master->id];
}

function get_masters_html( $master_list, $limit = 0, $delimiter = '<br/>' )
{
	$count = 0;
	$masters_full = array();
	$masters_short = array();
	foreach ($master_list as $master)
	{
		$count++;
		if ( $limit && $count <= $limit )
		{
			$masters_short[] = sprintf("<a href='./?mod=%s&act=%s&id=%d'>%s</a>", 'master', 'edit', $master->id, $master->name);
		}
		else
		{
			$masters_full[] = sprintf("<a href='./?mod=%s&act=%s&id=%d'>%s</a>", 'master', 'edit', $master->id, $master->name);
		}
	}
	if ( $masters_short && $masters_full )
	{
		$html = "<div class='masters short-list'>"
				. "<span class='short'>" . implode($delimiter, $masters_short) . "</span>"
				. "<span class='more'>" . $delimiter . " <a href='#more'>...</a></span>"
				. "<span class='full'>" . $delimiter . implode($delimiter, $masters_full) . "</span>"
				. "</div>";
	}
	elseif ( $masters_short )
	{
		$html = "<div class='masters short-list'>"
				. "<span class='short'>" . implode($delimiter, $masters_short) . "</span>"
				. "</div>";
	}
	else
	{
		$html = '';
	}
	return $html;
}